//
//  ShareView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/21.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "ShareView.h"
#import "ShareCell.h"
@interface ShareView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *bgBlackview;
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end



static NSString *shareCell = @"shareCell";
@implementation ShareView



- (instancetype)init{
    if (self = [super init]) {
        [self addData];
        [self loadSubviewsOnShareView];
    }
    
    return self;
}


- (void)addData{
    self.dataArray = [@[@{@"tint":@"微信好友",@"imageName":@"icon_fenxiang_weixin"},@{@"tint":@"微信朋友圈",@"imageName":@"icon__fenxiang_pengyouquan"},@{@"tint":@"手机QQ",@"imageName":@"icon_fenxiang_qq"},@{@"tint":@"QQ空间",@"imageName":@"icon_fenxiang_qqkongjian"},@{@"tint":@"新浪微博",@"imageName":@"icon_fenxiang_xinlang"}]mutableCopy];
}

- (void)loadSubviewsOnShareView{
    
    self.backgroundColor = [UIColor clearColor];
    self.window = [UIApplication sharedApplication].keyWindow;
    [self.window addSubview:self.bgBlackview];
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor clearColor];
    self.contentView = contentView;
    [self.window addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.window);
        make.height.mas_equalTo(200*HEIGHT);
    }];
    
    UIView *shareItemView = [[UIView alloc]init];
    shareItemView.backgroundColor = KLColor_C1;
    [contentView addSubview:shareItemView];
    shareItemView.layer.masksToBounds = YES;
    shareItemView.layer.cornerRadius = 10*WIDTH;
    [shareItemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(contentView);
        make.bottom.equalTo(contentView).offset(-20*HEIGHT);
    }];
    
    [contentView addSubview:self.collectionView];
    
    UIView *cancelView = [[UIView alloc]init];
    cancelView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:cancelView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCancel)];
    [cancelView addGestureRecognizer:tap];
    [cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(contentView);
        make.height.mas_equalTo(44*HEIGHT);
    }];
    
    UILabel *cancelTint = [[UILabel alloc]init];
    cancelTint.text = @"取消";
    cancelTint.textColor = KLColor_B12;
    cancelTint.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    [cancelView addSubview:cancelTint];
    [cancelTint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(cancelView);
    }];
    
}

- (void)tapCancel{
    [self cancel];
}

- (void)cancel{
    [self.bgBlackview removeFromSuperview];
    [self.contentView removeFromSuperview];
    [self removeFromSuperview];
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [layout setMinimumLineSpacing:30*HEIGHT];
        [layout setMinimumInteritemSpacing:0*WIDTH];
        [layout setItemSize:CGSizeMake(50*WIDTH, 80*HEIGHT)];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15*WIDTH, 40*HEIGHT,KLScreenW-15*WIDTH ,80*HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[ShareCell class] forCellWithReuseIdentifier:shareCell];
        //注册头视图
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = YES;
    }
    return _collectionView;
}

#pragma mark -- UICollectionViewDataSource
/** 每组cell的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ShareCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:shareCell forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor whiteColor];
//    [cell.titleBtn setTitle:self.dataArray[indexPath.row] forState:UIControlStateNormal];
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.logoImageView.image = [UIImage imageNamed:dic[@"imageName"]];
    cell.tint.text = dic[@"tint"];
    return cell;
}



#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第 %zd组 第%zd个",indexPath.section, indexPath.row);
    
    
}



- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (UIView *)bgBlackview{
    if (!_bgBlackview) {
        _bgBlackview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, KLScreenH)];
        _bgBlackview.backgroundColor = KLColor(0, 0, 0, 0.3);
    }
    return _bgBlackview;
}

@end
